/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelBillManage.java
 *
 * Created on Feb 14, 2012, 10:58:29 PM
 */
package GUI;

import Model.BillTableModel;
import Entity.Bill;
import Entity.ConnectionSQL;
import Entity.User;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
 *
 * @author Duc
 */
public class PanelBillManage extends javax.swing.JPanel {
    private User user;
    private ResourceBundle messages;
    private BillTableModel billTableModel;
    /** Creates new form PanelBillManage */
    public PanelBillManage(ResourceBundle messages,User user) {
        initComponents();
        this.user = user;
        this.messages = messages;
        billTable.setAutoCreateRowSorter(true);
        billTable.getTableHeader().setReorderingAllowed(false);
        viewAllBill();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        billTable = new javax.swing.JTable();
        btnNewBill = new javax.swing.JButton();
        btnDetail = new javax.swing.JButton();

        billTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        billTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                billTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(billTable);

        btnNewBill.setText("New Bill");
        btnNewBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewBillActionPerformed(evt);
            }
        });

        btnDetail.setText("Detail");
        btnDetail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetailActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(120, 120, 120)
                .addComponent(btnDetail)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE)
                .addComponent(btnNewBill)
                .addGap(134, 134, 134))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDetail)
                    .addComponent(btnNewBill))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnDetailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetailActionPerformed
         viewDetail();
    }//GEN-LAST:event_btnDetailActionPerformed

    private void btnNewBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewBillActionPerformed
        newBill();
    }//GEN-LAST:event_btnNewBillActionPerformed

    private void billTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_billTableMouseClicked
                if(evt.getClickCount()==2) viewDetail() ;
    }//GEN-LAST:event_billTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable billTable;
    private javax.swing.JButton btnDetail;
    private javax.swing.JButton btnNewBill;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
    public void viewAllBill(){
        ConnectionSQL cnn = new ConnectionSQL(messages);
        List<Bill> listBill = cnn.getBillByUser(user.getId());
        billTableModel = new BillTableModel(listBill, messages);
        billTable.setModel(billTableModel);
        billTableModel.fireTableDataChanged();
    }
    private void newBill(){
        ConnectionSQL cnn = new ConnectionSQL(messages);
        int billID = cnn.addBill(user);
        Bill bill = cnn.getBillByID(billID);
        billTableModel.Add(bill);
        billTableModel.fireTableDataChanged();
        new DialogAddBill(this, true, messages, user,bill).setVisible(true);
        billTableModel.fireTableDataChanged();
    }
    private void viewDetail() {
        int select  = billTable.getSelectedRow();
        if(select ==-1 ) JOptionPane.showMessageDialog(this, messages.getString("messagesNoSelect"));
        else{
            ConnectionSQL cnn = new ConnectionSQL(messages);
            int billID = Integer.parseInt(billTable.getValueAt(select, 0).toString());
            Bill bill  = cnn.getBillByID(billID);
            new DialogDetailBill(this, true, bill, user, messages).setVisible(true);
            billTableModel.fireTableDataChanged();
        }
    }
    public void fireDataChange(){
        billTableModel.fireTableDataChanged();
    }
    public void changeStatus(int billID,int status){
        billTableModel.changeStatus(billID, status);
    }
    public void removeBill(int billID){
        billTableModel.remove(billID);
    }
}
